package webmarket.test.selenium.common;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SeleniumTestHelper {
	private final WebDriver driver;
	private String baseUrl;
	private boolean acceptNextAlert = true;
	private StringBuffer verificationErrors = new StringBuffer();

	public SeleniumTestHelper() {
		driver = new FirefoxDriver();
		baseUrl = "http://localhost:8080/Administration";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	public void login() {
		/**
		 * Log in before each test
		 * 
		 * @throws Exception
		 */
		setUrl("/login/");
		getById("username").sendKeys("admin");
		getById("password").sendKeys("admin");
		getByName("login").click();
	}

	/**
	 * Sets the URL relative to the baseUrl
	 */
	public void setUrl(String relativeurl) {
		driver.get(baseUrl + relativeurl);
	}

	/**
	 * Gets the current url
	 */
	public String getUrl() {
		return driver.getCurrentUrl();
	}

	/**
	 * Gets an element by name
	 */
	public WebElement getByName(String name) {
		return driver.findElement(By.name(name));
	}

	/**
	 * Gets an element by id
	 */
	public WebElement getById(String id) {
		return driver.findElement(By.id(id));
	}

	/**
	 * Gets an element attribute by id
	 */
	public String getAttributeById(String id, String attribute) {
		return getById(id).getAttribute(attribute);
	}

	/**
	 * Gets an element css value by id
	 */
	public String getCssById(String id, String css) {
		return getById(id).getCssValue(css);
	}

	/**
	 * Gets an element attribute by name
	 */
	public String getAttributeByName(String name, String attribute) {
		return getByName(name).getAttribute(attribute);
	}

	/**
	 * Gets an element css value by name
	 */
	public String getCssByName(String name, String css) {
		return getByName(name).getCssValue(css);
	}

	/**
	 * Get element value by id
	 */
	public String getValueById(String id) {
		return getAttributeById(id, "value");
	}

	/**
	 * Get element value by name
	 */
	public String getValueByName(String name) {
		return getAttributeById(name, "value");
	}

	/**
	 * True, if given element is present in the document
	 */
	public boolean isElementPresentById(String id) {
		try {
			driver.findElement(By.id(id));
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
	
	/**
	 * True, if given element is present in the document
	 */
	public boolean isElementPresentByName(String name) {
		try {
			driver.findElement(By.name(name));
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
	
	

	/**
	 * True, if there is an alert present
	 */
	public boolean isAlertPresent() {
		try {
			driver.switchTo().alert();
			return true;
		} catch (NoAlertPresentException e) {
			return false;
		}
	}

	/**
	 * Closes the current alert and returns its text
	 */
	public String closeAlertAndGetItsText() {
		try {
			Alert alert = driver.switchTo().alert();
			String alertText = alert.getText();
			if (acceptNextAlert) {
				alert.accept();
			} else {
				alert.dismiss();
			}
			return alertText;
		} finally {
			acceptNextAlert = true;
		}
	}

	/**
	 * Closes the WebDriver, and returns the errors of the verification
	 */
	public String close() {
		driver.quit();
		return verificationErrors.toString();
	}

	/**
	 * Gets the WebDriver
	 */
	public WebDriver getDriver() {
		return driver;
	}
}
